const express = require('express')

const app = express()
const port = 3000

const db = require('./db/config.js');

app.use(express.static('public'))

app.get('/', (req, res) => res.send('Hello World!'))


// 新闻总数
app.get('/newstotal', (req, res) => {
    db.query(`SELECT * FROM news_table`, (err, result) => {
        if (err) throw err;
        let total = result.length;
        let responseData = {
            code: 0,
            count: total,
        }
        res.send(responseData)
    });
})

// 新闻列表接口
app.get('/getnews', (req, res) => {

    console.log(req.query); // { curr: '1', limit: '10' }

    let page = Number(req.query.curr) - 1   // 第几页
    let count = Number(req.query.limit)     // 每页多少条

    db.query(`SELECT * FROM news_table`, (err, result) => {
        if (err) throw err;
        let total = result.length;
        db.query(`SELECT title FROM news_table limit ${page * count},${count}`, (err, result) => {
            if (err) throw err;
            let responseData = {
                code: 0,
                count: total,
                listData: result
            }
            res.send(responseData)
        });
    });

})

app.listen(port, () => console.log(`Example app listening on port port!`))